package geektime.spring.springbucks.controller;

import geektime.spring.springbucks.model.Coffee;
import geektime.spring.springbucks.service.CoffeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/coffee")
public class Work1Controller {

    @Autowired
    private CoffeeService coffeeService;

    @RequestMapping("/queryAll/{pageNum:\\d+}:{pageSize:\\d+}")
    public ResponseEntity queryList(@RequestBody Map<String, String> args, @PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        return new ResponseEntity( coffeeService.queryAll(args,pageNum,pageSize), HttpStatus.OK);
    }

    /**
     * 查单条
     */
    @RequestMapping("queryById/{id}")
    public ResponseEntity queryById(@PathVariable("id") long id){
        return new ResponseEntity( coffeeService.queryById(id), HttpStatus.OK);
    }

    /**
     * 新增
     */
    @RequestMapping("insert")
    public ResponseEntity insert(@RequestBody Coffee coffee){
        return new ResponseEntity( coffeeService.insert(coffee), HttpStatus.OK);
    }
    /**
     *
     * 修改
     */
    @RequestMapping("update")
    public ResponseEntity update(@RequestBody Coffee coffee){
        return new ResponseEntity( coffeeService.update(coffee), HttpStatus.OK);
    }
    /**
     *
     * 删除
     */
    @RequestMapping("delete/{id}")
    public ResponseEntity delete(@PathVariable("id") long id){
        return new ResponseEntity( coffeeService.deleteById(id), HttpStatus.OK);
    }

}
